package com.ytg.myprocess.model;

import com.ytg.myanno.BindView;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;

/**
 * 被BindView注解标记的字段的模型类
 */
public class BindViewField {
    // VariableElement:表示一个字段、enum常量、方法或构造方法参数、局部变量或异常参数.
    // BindView注解针对的就是变量, 所以这里会将传入的element强转为VariableElement.
    public VariableElement mFieldElement;
    // 获取对应View的id.
    public int mResId;
    public String key;
//    public String methodGet;
//    public String methodSet;
//    public String bindTag;
    public BindViewField(Element element) throws IllegalArgumentException {
        if (element.getKind() != ElementKind.FIELD) { // 判断对应元素的类型是否为FIELD类型.
            throw new IllegalArgumentException(String.format("Only field can be annotated with @%s",
                    BindView.class.getSimpleName()));
        }
        mFieldElement = (VariableElement) element;
        BindView bindView = mFieldElement.getAnnotation(BindView.class);
        if(null!=bindView){
            mResId = bindView.value();
            key = bindView.key();
//            methodGet = bindView.methodGet();
//            methodSet = bindView.methodSet();
//            bindTag = bindView.sonTag();
            if (mResId < 0) {
                throw new IllegalArgumentException(String.format("value() in %s for field % is not valid",
                        BindView.class.getSimpleName(), mFieldElement.getSimpleName()));
            }
        }
    }
    // 获取被BindView注解的对象的变量名.
    public Name getFieldName() {
        return mFieldElement.getSimpleName();
    }

    public int getResId() {
        return mResId;
    }
    // 因为通过Element无法获取对应元素的具体信息, 所以需要通过asType获取对应的TypeMirror, 然后
    // 根据TypeMirror获取对应的元素信息.
    public TypeMirror getFieldType() {
        return mFieldElement.asType();
    }
}